<template>
  <div class="container">
    <el-card shadow="never">
      <div class="search-header">
        <el-row>
          <el-col :span="6">
            <el-form label-position="left" label-width="80px">
              <el-form-item label="关键字">
                <el-input
                  placeholder="根据编号搜索"
                  v-model="queryPage.keyword"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="18" class="choose-button">
            <div class="choose-btn">
              <el-button size="small" @click="clearInput">清除</el-button>
              <el-button size="small" type="primary" @click="searchItem"
                >搜索</el-button
              >
            </div>
          </el-col>
        </el-row>
      </div>
      <!-- 警告 -->
      <el-alert
        type="info"
        show-icon
        :closable="false"
        :title="'数据一共' + total + '条'"
      ></el-alert>
      <!-- 表格 -->
      <template>
        <el-table :data="randomTableData" style="width: 100%">
          <el-table-column align="center" label="编号" width="200">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="题型">
            <template slot-scope="scope">
              <span>
                {{
                  scope.row.questionType === '1'
                    ? '单选'
                    : scope.row.questionType === '2'
                    ? '多选'
                    : '简答'
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="题目编号" width="200">
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.questionIDs" :key="index">
                <el-button type="text" @click="showDialog(scope.row, item)">{{
                  item.number
                }}</el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            align="center"
            label="录入时间"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="totalSeconds"
            align="center"
            label="答题时间(s)"
          ></el-table-column>
          <el-table-column
            prop="accuracyRate"
            align="center"
            label="正确率(%)"
          ></el-table-column>
          <el-table-column
            prop="userName"
            align="center"
            label="录入人"
          ></el-table-column>
          <el-table-column align="center" :label="$t('table.actions')">
            <template slot-scope="scope">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                circle
                @click="deleteItem(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 表格 -->
      <!-- 分页 -->
      <el-pagination
        class="page-set"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryPage.page"
        :page-sizes="[5, 6, 8, 10]"
        :page-size="queryPage.pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="total - 0"
        background
      ></el-pagination>
    </el-card>
    <!-- 预览对话框 -->
    <el-dialog :visible.sync="dialogVisible" width="60%" title="题目预览">
      <preview-article
        :currentData="currentData"
        v-if="dialogVisible"
      ></preview-article>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import PreviewArticle from '../components/preview-article'
export default {
  components: { PreviewArticle },
  data() {
    return {
      total: '',
      // 组题列表数组
      randomTableData: [],
      queryPage: {
        page: 1,
        pagesize: 6,
        keyword: null
      },
      currentData: {},
      dialogVisible: false
    }
  },
  methods: {
    handleSizeChange(val) {
      this.queryPage.pagesize = val
      this.getRandomList()
    },
    handleCurrentChange(val) {
      this.queryPage.page = val
      this.getRandomList()
    },
    async getRandomList() {
      const res = await randoms(this.queryPage)
      console.log(res)
      this.total = res.data.counts
      this.randomTableData = res.data.items
    },
    async searchItem() {
      // 搜索
      this.getRandomList()
    },
    clearInput() {
      // 清除
      this.queryPage.keyword = null
      this.getRandomList()
    },
    deleteItem(val) {
      // 删除
      const id = val.id
      this.$confirm(`您确认删除此项?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await removeRandoms({ id })
          console.log(res)
          this.getRandomList()
          if (res.status == 200) {
            return this.$message.success('删除商品成功')
            this.getRandomList()
          } else {
            return this.$message.error('删除商品失败')
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        })
    },
    showDialog(row, item) {
      this.dialogVisible = true
      // const obj = { ...row };
      // obj.questionIDs = item;
      this.currentData = { id: item.id }
      console.log(row)
    }
  },
  created() {
    this.getRandomList()
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
a {
  color: rgb(0, 153, 255);
}
.el-card {
  margin: 10px 20px 0 10px;
  box-sizing: border-box;
}
.el-alert {
  margin-bottom: 15px;
  font-size: 16px;
}
.search-number .el-input {
  width: 240px;
  height: 32px;
  margin-left: 12px;
  float: right;
  box-sizing: border-box;
}

.choose-button .choose-btn {
  float: right;
  display: flex;
}
.page-set {
  margin: 20px 0;
  float: right;
}
</style>
